

// 百度地图API功能
var map = new BMap.Map('map');
var poi = new BMap.Point(111.281069,40.097977);    // 中心点 设置为 大路镇
map.centerAndZoom(poi, 13);
map.enableScrollWheelZoom();
var overlays = [];
var overlaycomplete = function(e){
    overlays.push(e.overlay);
    //点击后获取经纬度
    map.addEventListener("rightclick",function(e){
        $("#xy").val(e.point.lng + "," + e.point.lat);
        //alert(e.point.lng + "," + e.point.lat);
    });
};
var styleOptions = {
    strokeColor:"red",    //边线颜色。
    fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
    strokeWeight: 3,       //边线的宽度，以像素为单位。
    strokeOpacity: 0.8,    //边线透明度，取值范围0 - 1。
    fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
    strokeStyle: 'solid' //边线的样式，solid或dashed。
}
//实例化鼠标绘制工具
var drawingManager = new BMapLib.DrawingManager(map, {
    isOpen: false, //是否开启绘制模式
    enableDrawingTool: true, //是否显示工具栏
    drawingToolOptions: {
        anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
        offset: new BMap.Size(5, 5), //偏离值
    },
    circleOptions: styleOptions, //圆的样式
    polylineOptions: styleOptions, //线的样式
    polygonOptions: styleOptions, //多边形的样式
    rectangleOptions: styleOptions //矩形的样式
});
//添加鼠标绘制工具监听事件，用于获取绘制结果
drawingManager.addEventListener('overlaycomplete', overlaycomplete);
function clearAll() {
    for(var i = 0; i < overlays.length; i++){
        map.removeOverlay(overlays[i]);
    }
    overlays.length = 0
}
function sector1Test () {
    console.log('Sector1Test');
    var center = new BMap.Point(111.281069,40.097977);
    var polygon =  sector1(center,10000,90,120,'black',1,0,'black',0);
    map.addOverlay(polygon);   //增加多边形
}

//以画多边形区域的方法画扇形区域 画出以point2点为圆心，半径为radius，夹角从sDegree到eDegree的扇形
function sector1(point2, radius, sDegree, eDegree, strokeColour, strokeWeight, Strokepacity, fillColour, fillOpacity, opts) {

    var points = []; //创建构成多边形的点数组
    var step = ((eDegree - sDegree) / 10) || 10; //根据扇形的总夹角确定每步夹角度数，最大为10
    points.push(point2);

    for (var i = sDegree; i < eDegree + 0.001; i += 1) { //循环获取每步的圆弧上点的坐标，存入点数组
        points.push(EOffsetBearing(point2, radius, i));
    }
    points.push(point2);
    console.log("半径上点坐标"+"x:"+points[points.length-1]+"，y:"+points[1]);
    //根据构成的点数组以及其他参数画多边形
    var polygon = new BMap.Polygon(
        points
        , { strokeColor: strokeColour, strokeWeight: strokeWeight, strokeOpacity: Strokepacity, fillColor: fillColour, fillOpacity: fillOpacity });

    if (sDegree == eDegree) {
        var planepoint = points[1];
        var myIcon = new BMap.Icon("../images/uav.png",
            new BMap.Size(36, 36));
        var marker = new BMap.Marker(planepoint, {
            icon: myIcon
        });
        var infoWindow = new BMap.InfoWindow("信息:", opts); // 创建信息窗口对象
        marker.addEventListener("click", function () {
            map.openInfoWindow(infoWindow, planepoint); // 开启信息窗口
            //window.external.Test(sDegree);
        });
        map.addOverlay(marker);


    }

    var opts = {
        width: 230,
        height: 100,
        title: '经纬度'
    };
    console.log("圆心坐标:"+points[points.length-1].lat+","+points[points.length-1].lng);
    console.log("端坐标1:"+points[1].lat+","+points[1].lng);
    console.log("端坐标2:"+points[points.length-2].lat+","+points[points.length-2].lng);
    var marker1 = new BMap.Marker(points[1]);
    var infoWindow1 = new BMap.InfoWindow("经度:"+points[1].lat+"，纬度:"+points[1].lng, opts); // 创建信息窗口对象
    marker1.addEventListener("click", function () {
        map.openInfoWindow(infoWindow1, points[1]); // 开启信息窗口
    });
    var marker2 = new BMap.Marker(points[points.length-1]);
    var infoWindow2 = new BMap.InfoWindow("经度:"+points[points.length-1].lat+"，纬度:"+points[points.length-1].lng, opts); // 创建信息窗口对象
    marker2.addEventListener("click", function () {
        map.openInfoWindow(infoWindow2, points[points.length-1]); // 开启信息窗口
    });
    var marker3 = new BMap.Marker(points[points.length-2]);
    var infoWindow3 = new BMap.InfoWindow("经度:"+points[points.length-2].lat+"，纬度:"+points[points.length-2].lng, opts); // 创建信息窗口对象
    marker3.addEventListener("click", function () {
        map.openInfoWindow(infoWindow3, points[points.length-2]); // 开启信息窗口
    });
    map.addOverlay(marker1);
    map.addOverlay(marker2);
    map.addOverlay(marker3);
    return polygon;
}

//使用数学的方法计算需要画扇形的圆弧上的点坐标
function EOffsetBearing(point3, dist, bearing) {
    var lngConv = map.getDistance(point3, new BMap.Point(point3.lng + 0.1, point3.lat)) * 10;  //计算1经度与原点的距离
    var latConv = map.getDistance(point3, new BMap.Point(point3.lng, point3.lat + 0.1)) * 10;  //计算1纬度与原点的距离
    var lat = dist * Math.sin(bearing * Math.PI / 180) / latConv;  //正弦计算待获取的点的纬度与原点纬度差
    var lng = dist * Math.cos(bearing * Math.PI / 180) / lngConv;  //余弦计算待获取的点的经度与原点经度差
    return new BMap.Point(point3.lng + lng, point3.lat + lat);
}